package com.vtmer.controller;

import com.vtmer.entity.IntervStatus;
import com.vtmer.entity.Progress;
import com.vtmer.entity.RegistrationForm;
import com.vtmer.result.ResultVo;
import com.vtmer.result.impl.ResultCode;
import com.vtmer.service.ProgressService;
import com.vtmer.service.RegistrationFormService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author Xinjie
 * @date 2022/9/26 10:43
 */
@RestController
public class UserController {
    @Resource
    RegistrationFormService registrationFormService;
    @Resource
    ProgressService progressService;

    /**
     * 根据用户id获取报名信息
     *
     * @param uid 用户id
     * @return 返回报名表信息, 若没有报名则返回null
     */
    private RegistrationForm getRegistrationFormById(int uid) {
        return registrationFormService.getFormByUid(uid);
    }

    @GetMapping("/getForm/{uid}")
    public ResultVo<RegistrationForm> getForm(@PathVariable int uid) {
        Optional<RegistrationForm> optionalRegistrationForm = Optional.ofNullable(getRegistrationFormById(uid));
        return optionalRegistrationForm.map(ResultVo::new).orElseGet(() -> new ResultVo<>(ResultCode.NO_INFO));
    }

    /**
     * 根据用户id获取面试流程信息
     *
     * @param uid 用户id
     * @return 返回获取到的流程信息
     */
    private Progress getProgressById(int uid) {
        return progressService.getProgressByUid(uid);
    }


    private Progress getProgressByStudId(long studId){
        return progressService.getProgressByStudId(studId);
    }



    @GetMapping("/getProgress/{studId}")
    public IntervStatus getProgress(@PathVariable long studId) {
        RegistrationForm registrationForm = getProgressByStudIdAndName(studId);
        Assert.notNull(registrationForm,"没有该用户");
        return new IntervStatus(registrationForm);
    }

    private RegistrationForm getProgressByStudIdAndName(@PathVariable long studId){
        return registrationFormService.getProgressByStudIdAndName(studId);
    }

    /**
     * 管理员获取所有报名表
     *
     * @return 报名表list
     */
    private List<RegistrationForm> getRegistrationFormList() {
        return registrationFormService.getFormList();
    }

    @GetMapping("/administrators/getForm")
    public ResultVo<List<RegistrationForm>> getFormList() {
        Optional<List<RegistrationForm>> optionalRegistrationForm = Optional.ofNullable(getRegistrationFormList());
        return optionalRegistrationForm.map(ResultVo::new).orElseGet(() -> new ResultVo<>(ResultCode.NO_INFO));
    }

    /**
     * 管理员根据组别查询报名信息
     *
     * @param groups 组别
     * @return 返回查询到的报名信息
     */
    private List<RegistrationForm> getRegistrationFormByGroup(String groups) {
        return registrationFormService.getFormByGroup(groups);
    }

    @GetMapping("/administrators/getForm/{groups}")
    public ResultVo<List<RegistrationForm>> getFormByGroup(@PathVariable String groups) {
        Optional<List<RegistrationForm>> optionalRegistrationForm = Optional.ofNullable(getRegistrationFormByGroup(groups));
        return optionalRegistrationForm.map(ResultVo::new).orElseGet(() -> new ResultVo<>(ResultCode.NO_INFO));
    }

    /**
     * 管理员获取全部面试流程列表
     *
     * @return 所有面试流程信息
     */
    private List<Progress> getProgress1() {
        return progressService.getProgressList();
    }

    @GetMapping("/administrators/getProgress")
    public ResultVo<List<Progress>> getProgressList() {
        Optional<List<Progress>> optionalProgresses = Optional.ofNullable(getProgress1());
        return optionalProgresses.map(ResultVo::new).orElseGet(() -> new ResultVo<>(ResultCode.NO_INFO));
    }

    /**
     * 管理员根据组别获取信息流程表
     *
     * @param groups 组别
     * @return 所有面试流程信息
     */
    private List<Progress> getProgress2(String groups) {
        return progressService.getProgressListByGroups(groups);
    }

    /**
     * 管理员根据用户学号更新已报名
     *
     * @param studId 学号
     * @return 面试结果
     */
    @PutMapping("/administrators/updateIntervStatus/{studId}")
    private IntervStatus updateIntervStatus(@PathVariable String studId, @RequestParam int state) {
        return progressService.updateIntervStatusByStudId(studId,state);
    }

    /**
     * 管理员根据用户学号更新面试通过
     *
     * @param studId 学号
     * @return 面试结果
     */
    @PutMapping("/administrators/updateIntervResult/{studId}")
    private Progress updateIntervResult(@PathVariable String studId) {
        return progressService.updateIntervResultByStudId(studId);
    }

    /**
     * 管理员根据用户学号更新一轮通过
     *
     * @param studId 学号
     * @return 一轮结果
     */
    @PutMapping("/administrators/updateFirAssessmResult/{studId}")
    private Progress updateFirAssessmResult(@PathVariable String studId) {
        return progressService.updateFirAssessmResultByStudId(studId);
    }

    /**
     * 管理员根据用户学号更新二轮结果
     *
     * @param studId 学号
     * @return 二轮结果
     */
    @PutMapping("/administrators/updateSecAssessmResult/{studId}")
    private Progress updateSecAssessmResult(@PathVariable String studId) {
        return progressService.updateSecAssessmResultByStudId(studId);
    }

    @GetMapping("/administrators/getProgress/{groups}")
    public ResultVo<List<Progress>> getProgressByGroups(@PathVariable String groups) {
        Optional<List<Progress>> optionalProgresses = Optional.ofNullable(getProgress2(groups));
        return optionalProgresses.map(ResultVo::new).orElseGet(() -> new ResultVo<>(ResultCode.NO_INFO));
    }

    /**
     * 管理员根据姓名获取报名报信息
     *
     * @param username 用户名
     * @return 报名表
     */
    @GetMapping("/administrators/getFormByName/{username}")
    public ResultVo<RegistrationForm> getRegistrationFormByName(@PathVariable String username) {
        Optional<RegistrationForm> optionalRegistrationForm = Optional.ofNullable(registrationFormService.getRegistrationFormByName(username));
        return optionalRegistrationForm.map(ResultVo::new).orElseGet(() -> new ResultVo<>(ResultCode.NO_INFO));
    }

    /**
     * 管理员根据学号获取报名报信息
     *
     * @param studId 学号
     * @return 报名表
     */
    @GetMapping("/administrators/getFormByStudId/{studId}")
    public ResultVo<RegistrationForm> getRegistrationFormByStudId(@PathVariable String studId) {
        Optional<RegistrationForm> optionalRegistrationForm = Optional.ofNullable(registrationFormService.getRegistrationFormByStudId(studId));
        return optionalRegistrationForm.map(ResultVo::new).orElseGet(() -> new ResultVo<>(ResultCode.NO_INFO));
    }
}
